import CryptoJS from '../../3rd/crypto-js.js';
/**
 * aes 加密对秘钥有长度要求(16,24,32)
 */
const aes_key = "73894DF71D5B0816C540AB6AE47F07FC";
/**
 * 加密解密
 */
export class CryptoUtil {
    /**
     * MD5
     * @param data 
     * @returns 
     */
    public static MD5(data: string): string {
        return CryptoJS.MD5(data).toString().toUpperCase();
    }

    /**
     * aes 加密
     * @param data 
     * @param key 
     * @returns 
     */
    public static aes_encode(data: string, key: string = aes_key): string {
        const options = {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        };
        const encryptedData = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(data), CryptoJS.enc.Utf8.parse(key), options);
        return encryptedData.toString();
    }

    /**
     * 解密
     * @param data 
     * @param key 
     * @returns 
     */
    public static aes_decode(data: string, key: string = aes_key): string {
        const options = {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        };
        const decryptedData = CryptoJS.AES.decrypt(data, CryptoJS.enc.Utf8.parse(key), options);
        const decryptedStr = CryptoJS.enc.Utf8.stringify(decryptedData);
        return decryptedStr.toString();
    }
}